// pages/orderDetail/index.js
const app = getApp()
import {
    util,
    Toast,
    Dialog,
    commonMixin
} from "../../utils/util.js";
Page({
    behaviors: [commonMixin],

    /**
     * 页面的初始数据
     */
    data: {
        userId: 0,
        sampleId: 0,
        vertical: false,
        autoplay: false,
        interval: 2000,
        duration: 500,
        swiperIndex: 0,
        downloadAll: true,
        order: {
            payment_status: 0,
            status: 0,
            samples: []
        },
        top_imgs: [
            'https://img01.yzcdn.cn/vant/cat.jpeg',
            'https://img01.yzcdn.cn/vant/cat.jpeg',
            'https://img01.yzcdn.cn/vant/cat.jpeg'
        ], //头部banner、
        topGoods: [],
    },
    swiperChange(e) {
        this.setData({
            swiperIndex: e.detail.current
        })
    },
    //复制订单号
    copy(e) {
        let copytxt = e.currentTarget.dataset.order;
        wx.setClipboardData({
            data: copytxt,
            success(res) {
                
            }
        })
    },

    handleCodeCopy(e) {
      let copytxt = e.currentTarget.dataset.code;
      wx.setClipboardData({
        data: copytxt,
        success(res) {
          Toast("检测码拷贝成功！");
        }
      })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let userInfo = util.getUserInfo();
        this.setData({
            sampleId: options.id,
            userId: userInfo.id
        });
        console.log('userInfo', userInfo);
        util.http.post("v1/mini/home-selected-goods",{}).then((res) => {
            if (res.data.code == 0) {
              this.setData({
                topGoods: res.data.data
              })
            } else {
                Toast(res.data.message);
            }
            wx.hideLoading();
        }).catch((err) => {
            wx.hideLoading();
            Toast('网络异常！刷新后重试！');
            console.log(err)
        })
    },

    loadSample() {
        let id = this.data.sampleId;
        wx.showLoading({
            title: '加载中',
          })
          util.http.post("v1/mini/get-order",{
              id: id
          }).then((res) => {
              if (res.data.code == 0) {
                var samples = res.data.data.samples;
                let showAllBtn = false;
                for (let i = 0; i < samples.length; i++) {
                  if (samples[i].is_used == 0) {
                    showAllBtn = true;
                    break;
                  }
                }
                this.setData({
                  order: res.data.data,
                  downloadAll: showAllBtn
                })
              } else {
                  Toast(res.data.message);
                  wx.navigateTo({
                    url: '/pages/index/index',
                  })
              }
              wx.hideLoading();
          }).catch((err) => {
              wx.hideLoading();
              Toast('网络异常！刷新后重试！');
              console.log(err)
          })
    },

    handlePreviewQrcode(event) {
        wx.previewImage({
            urls: [event.currentTarget.dataset.url]
        })
    },

    handleDownloadQrcodeFiles(event) {
      let imageUrls = [];
      for (let i = 0; i < this.data.order.samples.length; i++) {
        let sample = this.data.order.samples[i];
        if (sample.is_used == 0) {
          imageUrls.push(sample.qrcode.url);
        }
      }
      util.batchDownloadImages(imageUrls);
        // let id = this.data.sampleId;
        // util.saveFile('v1/mini/download-order-qrcode-files?id='+id);
    },

    handleJumpCheck() {
        const sample = this.data.order.samples[this.data.swiperIndex]
        console.log(sample.sample_sn);
        wx.navigateTo({
            url: '/pages/check/index?sampleSn='+sample.sample_sn,
        })
    },

    handleModifyCheck() {
      const sample = this.data.order.samples[this.data.swiperIndex]
      console.log(sample.sample_sn);
      wx.navigateTo({
          url: '/pages/check/index?is_modify=1&sampleSn='+sample.sample_sn,
      })
    },

    handleBindUser() {
        const sample = this.data.order.samples[this.data.swiperIndex]
        wx.showLoading({})
        util.http.post("v1/mini/sample-bind-user",{
            sample_sn: sample.sample_sn
        }).then((res) => {
            if (res.data.code == 0) {
                wx.navigateTo({
                    url: '/pages/check/index?id='+sample.id,
                })
            } else {
                Toast(res.data.message);
            }
            wx.hideLoading();
        }).catch((err) => {
            wx.hideLoading();
            Toast('网络异常！刷新后重试！');
            console.log(err)
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.loadSample();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})